package net.zjitc.controller;

import net.zjitc.domain.User;
import net.zjitc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping
    public String say() {
        return "hello";
    }

    @PostMapping
    public Boolean add(@RequestBody User user) {
        try {
            userService.add(user);
        } catch (Exception e){
            return false;
        }
        return true;
    }
    @DeleteMapping("/{id}")
    public Boolean deleteById(@PathVariable("id") Integer id) {
        try {
            userService.deleteById(id);
        } catch (Exception e) {
            return false;
        }
        return true;
    }
    @GetMapping("/{id}")
    public User findById(@PathVariable("id") Integer id) {
        return userService.findById(id);
    }
    @PutMapping
    public Boolean update(@RequestBody User user) {
        return userService.updateUser(user);
    }
    @PostMapping("/{currentPage}/{pageSize}")
    public List<User> getPage(@PathVariable("currentPage") int currentPage,
                              @PathVariable("pageSize") int pageSize,
                              @RequestBody User user) {
        return userService.findPage(currentPage,pageSize,user);
    }
}
